<?php 
namespace app\Admin\controller;
use think\Controller;
use think\Db;
use think\Request;
use  think\File;
use app\Admin\model\Trademark;
class Brand extends Base
{	
	//品牌列表
	public function brand_list()
	{	
		$brand_name = input('post.brand_name');
		$where = [];
		if(!empty($brand_name)){
			$where['brand_name'] = ['like',"%$brand_name%"];
		}
		$list = Db("brand")->where($where)->paginate();
		$this->assign("list",$list);
		return $this->fetch();
	}
	//添加品牌
	public function brand_add()
	{
		if(request()->isPost()) {
			$data['brand_name'] = input("post.brand_name");
			$data['site_url'] = input("post.site_url");
			$file = request()->file("brand_logo");
			$data['brand_logo'] = $this->upload($file);
			$data['brand_desc'] = input("post.brand_desc");
			$obj = new Trademark();
			$res = $obj->addbrand($data);
			$this->redirect('Brand/brand_list');
		}
		return $this->fetch();
	}

	public function upload($file)
	{    
	    if($file){
	        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
	        if($info){
	             $a=$info->getSaveName();
	             $imgp= str_replace("\\","/",$a);
	             $imgpath='/uploads/'.$imgp;
	       
	            return $imgpath;
	          
	        }else{   
	            return $file->getError();
	        }
	    }
	}


}
